package com.qianbei.view.bluetooth;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * <pre>
 *     author : pipi
 *     e-mail : xxx@xx
 *     time   : 2018/08/17
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class Utils {
    private static final String TAG = Utils.class.getSimpleName();

    public static char crc16Calc(byte[] datas, int len, char crc16_type) {
        char crc16;
        byte crc_h4;
        final char CRCTable[] = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6,
                0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF};
        crc16 = (char) (0xffff - crc16_type);
        for (int i = 0; i < len; i++) {
            crc_h4 = (byte) (crc16 >> 12);
            crc16 = (char) (crc16 << 4);
            crc16 = (char) (crc16 ^ CRCTable[crc_h4 ^ ((datas[i] & 0xff) >> 4)]);
            crc_h4 = (byte) (crc16 >> 12);
            crc16 = (char) (crc16 << 4);
            crc16 = (char) (crc16 ^ CRCTable[crc_h4 ^ (datas[i] & 0x0f)]);
        }
        return crc16;
    }

    /**
     * @param list 将被转换为byte[]的List<Byte>
     * @return 转换成的byte数组
     */
    public static byte[] list2byte(List<Byte> list) {
        if (list == null || list.size() < 0)
            return null;
        byte[] bytes = new byte[list.size()];
        int i = 0;
        Iterator<Byte> iterator = list.iterator();
        while (iterator.hasNext()) {
            bytes[i] = iterator.next();
            i++;
        }
        return bytes;
    }

    public static String byte2Str(byte[] data_byte) {
        StringBuilder sb = new StringBuilder();
        for (byte b : data_byte) {
            sb.append(Integer.toHexString(b & 0xff));
            sb.append("  ");
        }
        return sb.toString();
    }
    @SuppressLint("SimpleDateFormat")
    public static String getNowTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }

    public static boolean isNumeric(String str){
        if(str.equals("")){
            return false;
        }
        for (int i = 0; i < str.length(); i++){
            System.out.println(str.charAt(i));
            if (!Character.isDigit(str.charAt(i))){
                return false;
           }
        }
      return true;
    }
}
